/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ddbserver.server;

import ddbserver.common.ExceptionHandler;
import ddbserver.DDBServerView;
import ddbserver.connections.GDDManager;
import ddbserver.connections.SiteManager;
import ddbserver.connections.SocketConnector;
import ddbserver.connections.TableManager;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;

/**
 *
 * @author Roar
 */
public class ServerUtil extends Thread {

    private DDBServerView mainWindow;
    private String ip;
    private int port;
    private String name;
    private ServerSocket serverSocket;
    private boolean running = false;
    SiteManager siteManager;
    TableManager tableManager;
    GDDManager gddManager;

    public boolean isRunning() {
        return running;
    }

    public void setRunning(boolean running) {
        this.running = running;
    }

    public ServerUtil(DDBServerView mainWindow, String ip, int port, String name) {
        super();
        this.mainWindow = mainWindow;
        this.ip = ip;
        this.port = port;
        this.name = name;
        try {
            serverSocket = new ServerSocket(port, 0, InetAddress.getByName(ip));
            serverSocket.setSoTimeout(1000);
        } catch (Exception e) {
            ExceptionHandler.handleExcptin(e);
        }
        this.siteManager = SiteManager.getInstance();
        SiteManager.getInstance().setMyip(ip);
        SiteManager.getInstance().setMyport(port);
        SiteManager.getInstance().setMyname(name);
        this.tableManager = TableManager.getInstance();
        this.gddManager = GDDManager.getInstance();
    }

    @Override
    public void run() {
        super.run();
        running = true;
        mainWindow.setText("Server Started.\r\n");
        while (running) {
            try {
                Socket socket = serverSocket.accept();
                SocketConnector socketConnector = new SocketConnector(socket);
                String str = socketConnector.read();
                if (str.equalsIgnoreCase("admin")) {
                    mainWindow.setText("admin from " + socket.getInetAddress().toString());
                    AdminUtil adminUtil = new AdminUtil(socketConnector, mainWindow, name);
                    adminUtil.run();
                } else if (str.equalsIgnoreCase("client")) {
                    mainWindow.setText("client from " + socket.getInetAddress().toString());
                    ClientUtil clientUtil = new ClientUtil(socketConnector, mainWindow, name);
                    clientUtil.run();
                } else {
                    mainWindow.setText(str + " is not a ddbadmin or server.");
                }
            } catch (SocketTimeoutException e) {
                //e.printStackTrace();
            } catch (Exception e) {
                ExceptionHandler.handleExcptin(e);
            }
        }
        try {
            serverSocket.close();
        } catch (Exception e) {
            ExceptionHandler.handleExcptin(e);
        }
        mainWindow.setText("Server stopped.");
    }
}
